﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CapstoneProjectAEReader.Entities.Models;
using CapstoneProjectAEReader.Common.Utility;
using CapstoneProjectAEReader.DataAccess.DBAccess;
using UserControl = System.Windows.Controls.UserControl;
using CapstoneProjectAEReader.GUIs.ReaderGUI;

namespace CapstoneProjectAEReader.GUIs.Others
{
    public partial class Blank : Window
    {
        #region Attributes

        private ReaderGui _parentGui;
        private Note _note = new Note();
        private DAO _dao;
        private NoteDetail _noteDetail = new NoteDetail();
        private const string BtSaveName = "saveButton";
        private const string BtCancelName = "cancelButton";
        private const string AddNoteSuccessfully = "addSuccessNote";
        private const string PageNumber = "pageNo";
        private const string InvalidPageNo = "invalidPageNo";

        #endregion 

        public Blank(string note, int bookId, DTO dto, int pageNo, ReaderGui parent)
        {
            _parentGui = parent;
            _note.NoteContent = note;
            _dao = new DAO(dto);
            _noteDetail.Bookid = bookId;
            _noteDetail.PageNumber = pageNo;
            InitializeComponent();
            ReloadFormLanguage();
            RebindData();
        }

        public Blank(Note note, int bookId, DTO dto,ReaderGui parent)
        {
            _parentGui = parent;
            _note = note;
            _dao = new DAO(dto);
            _noteDetail = _dao.GetNoteDetail(_note.NoteId, bookId);
            InitializeComponent();
            ReloadFormLanguage();
            RebindData();
        }

        public void ReloadFormLanguage()
        {
            btCancelName.Content = ResourceReader.GetResourceContent(BtCancelName);
            btSaveName.Content = ResourceReader.GetResourceContent(BtSaveName);
        }

        public void RebindData()
        {
            lbNoteWord.Text = _note.NoteContent;
            _noteDetail.Createdate = DateTime.Now.ToString(Constants.FullDateTimeFormatToSecond);
            lbBookTitle.Content = _dao.GetEbookById(_noteDetail.Bookid).Title;
            lbDate.Content = _noteDetail.Createdate;
            lbPageNo.Content = ResourceReader.GetResourceContent(PageNumber);
            if (_noteDetail.PageNumber > 0)
            {
                txtPageNo.Text = _noteDetail.PageNumber.ToString();
            }
            txtNoteDescription.Document = new FlowDocument(new Paragraph(new Run(_noteDetail.Description)));
        }

        public void btSaveHandler(object sender, RoutedEventArgs e)
        {
            int noteId;
            int pageNo;
            if (int.TryParse(txtPageNo.Text,out pageNo))
            {
                if (pageNo < 0 || pageNo > _dao.GetEbookById(_noteDetail.Bookid).PageCount)
                {
                    Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(InvalidPageNo));
                }
                else
                {
                    if (_dao.CheckNoteExist(_note.NoteContent))
                    {
                        _note = _dao.GetNote(_note.NoteContent);
                        noteId = _note.NoteId;
                    }
                    else
                    {
                        noteId = _dao.InsertNote(_note);
                    }
                    _noteDetail.PageNumber = pageNo;
                    _noteDetail.Noteid = noteId;
                    var textRange = new TextRange(txtNoteDescription.Document.ContentStart, txtNoteDescription.Document.ContentEnd);
                    _noteDetail.Description = textRange.Text;
                    if(!_dao.CheckNoteDetailExist(_noteDetail.Noteid,_noteDetail.Bookid))
                    {
                        _dao.InsertNoteDetail(_noteDetail);    
                    }
                    else
                    {
                        _dao.UpdateNoteDetail(_noteDetail);
                    }
                    Close(); 
                }
            }
            else
            {
                Utilities.ShowWarningMesbox(ResourceReader.GetResourceContent(InvalidPageNo));
            }
        }

        public void btCancelHandler(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            _parentGui.RebindData();
        }
    }
}
